package org.ade.monak.anak.service.gate.monak;

import org.ade.monak.anak.Variable.TipePesanMonak;
import org.ade.monak.anak.entity.DataMonitoring;
import org.ade.monak.anak.entity.IPesanData;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.service.DatabaseThreadService;
import org.ade.monak.anak.service.storage.DatabaseManagerAnak;

import android.content.Context;

public class ReceiverPesanData {
	
	
	public void menerimaPesanData(final Context context, DatabaseThreadService databaseThread, IPesanData pesanData, final Ortu ortu){
		if(pesanData!=null){
			final DataMonitoring dataMonitoring = (DataMonitoring) pesanData;
			dataMonitoring.setOrtu(ortu);
			switch(pesanData.getTipe()){
				case TipePesanMonak.DATAMONITORING_BARU:{
					databaseThread.addDatabaseThread(new Runnable() {
						public void run() {
							dataMonitoring.setAktif(true);
							new DatabaseManagerAnak(context).addDataMonitoring(dataMonitoring);
						}
					});
				
					SenderKonfirmasi sender = new SenderKonfirmasi(context, ortu);
					sender.sendKonformasiAktifMonitoring(dataMonitoring.getIdMonitoring());
					break;
				}case TipePesanMonak.DATAMONITORING_UPDATE:{
					databaseThread.addDatabaseThread(new Runnable() {
						public void run() {
							dataMonitoring.setAktif(true);
							DatabaseManagerAnak databaseManager = new DatabaseManagerAnak(context);
							DataMonitoring dataMonitoringDatabase = databaseManager.getDataMonitoringByIdMonitoringAndOrtu(dataMonitoring.getIdMonitoring(), ortu.getIdOrtu(), false, false);
							if(dataMonitoringDatabase!=null){
								databaseManager.updateDataMonitoring(dataMonitoring);
							}else{
								databaseManager.addDataMonitoring(dataMonitoring);
							}
						}
					});
					
					SenderKonfirmasi sender = new SenderKonfirmasi(context, ortu);
					sender.sendKonformasiAktifMonitoring(dataMonitoring.getIdMonitoring());
					break;
				}case TipePesanMonak.DATAMONITORING_DELETE:{
					databaseThread.addDatabaseThread(new Runnable() {
						public void run() {
							new DatabaseManagerAnak(context).deleteDataMonitoring(dataMonitoring, ortu.getIdOrtu());
						}
					});
					break;
				}
			}
		}
	}
	

	
}
